package com.soft.dao;

import com.soft.pojo.Notice;
import com.soft.utils.DBUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class NoticesDAO {
    public Notice showNewNotice() {
        List<Notice> notices = DBUtil.query("select * from notice ORDER BY n_time desc", Notice.class);
        if (notices.size() > 0) {
            return notices.get(0);
        }
        return null;
    }

    public List<Notice> selectAllNotices() {
        List<Notice> noticeList = DBUtil.query("select * from notice", Notice.class);
        return noticeList;
    }

    public void insertNotices(Notice notice) {
        DBUtil.excute("insert into notice(title,details,n_time) values(?,?,?)", notice.getTitle(), notice.getDetails(), notice.getN_time());
    }

    public void delNotices(int id) {
        DBUtil.excute("delete from notice where n_id = ?", id);
    }

    public Notice selectNoticesByID(int id) {
        List<Notice> notice = DBUtil.query("select * from notice where n_id = ?", Notice.class, id);
        if (notice.size()>0){
            return notice.get(0);
        }
        return null;
    }

    public void updateNotices(Notice notice) {
        String Nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println(Nowtime);
        DBUtil.excute("update notice set title=?,details=?,n_time=? where n_id=?", notice.getTitle(), notice.getDetails(), Nowtime, notice.getN_id());
    }
}
